/*
 * @Author: C.
 * @Date: 2023-01-29 14:50:53
 */
// type mapType = 'table' | 'form' | 'des';
import { BasicColumn } from '/@/components/Table/src/types/table';
import { FormSchema } from '/@/components/Form/index';
import { DescItem } from '/@/components/Description/index';
import { dataFunc, dataRecordFunc } from '../data/index';
import tableMaps, { tableFieldType } from '/@/jv_package/maps/fields/tableMaps';
import formMaps, { formFieldType } from '/@/jv_package/maps/fields/formMaps';
import descMaps, { descFieldType } from '/@/jv_package/maps/fields/descMaps';
import { FilterEnums } from '../../maps/filters/index';
export interface MoreProps {
  filter?: FilterEnums;
  componentProps?: Recordable;
}
function getFields<T>(fields, maps, sign: string): T[] {
  if (!fields || fields.length == 0) return [];
  const result: T[] = [];
  function pushField(field) {
    // result.push(field);
    if (sign === 'table') {
      if (!Reflect.has(field, 'format')) {
        result.push({
          width: 150,
          editRender: field.edit ? dataRecordFunc(field) : undefined,
          format: dataFunc(field),
          ...field,
        });
        return;
      }
    } else if (sign === 'desc') {
      if (!Reflect.has(field, 'render')) {
        result.push({
          render: dataFunc(field),
          ...field,
        });
        return;
      }
    }
    result.push(field);
  }
  fields.forEach((item) => {
    // 字符串
    if (typeof item == 'string') {
      maps[item] && pushField(maps[item]);
      // 数组
    } else if (Object.prototype.toString.call(item) == '[object Array]') {
      const key = item[0];
      const customSchema = item[1];
      pushField(Object.assign({}, maps[key], customSchema));
    } else {
      // 其他
      pushField(item);
    }
  });
  return result;
}
export function getTableMaps(
  fields: (
    | tableFieldType
    | (BasicColumn & MoreProps)
    | [tableFieldType, Partial<Omit<BasicColumn & MoreProps, 'dataIndex'>>]
  )[],
): BasicColumn[] {
  return getFields(fields, tableMaps, 'table');
}
export function getFormMaps(
  fields: (formFieldType | FormSchema | [formFieldType, Partial<Omit<FormSchema, 'field'>>])[],
): FormSchema[] {
  return getFields(fields, formMaps, 'form');
}
export function getDescMaps(
  fields: (
    | descFieldType
    | (DescItem & MoreProps)
    | [descFieldType, Partial<Omit<DescItem & MoreProps, 'field'>>]
  )[],
): DescItem[] {
  return getFields(fields, descMaps, 'desc');
}
